package com.gitee.threefish.idea.json.components;

/**
 * @author Nekio
 */

// <editor-fold defaultstate="collapsed" desc="Libraries">

import org.json.JSONObject;

import javax.swing.*;
import java.awt.*;

public class TreeParent extends JPanel {
    private TreeChild tree;

    private JButton btnCollapseTree;
    private JButton btnExpandTree;

    private JSONObject root;

    public TreeParent(JSONObject root) {
        if (root != null) {
            this.root = root;
            addComponents();
            addListeners();
        }
    }

    private void addComponents() {
        this.setLayout(new BorderLayout());
        JPanel pnlTreeButtons = new JPanel(new GridLayout(1, 2));
        btnCollapseTree = new JButton("收起全部");
        pnlTreeButtons.add(btnCollapseTree);
        btnExpandTree = new JButton("展开全部");
        pnlTreeButtons.add(btnExpandTree);
        this.add(pnlTreeButtons, BorderLayout.NORTH);
        tree = null;
        tree = new TreeChild(root);
        tree.setOpaque(false);
        this.add(tree, BorderLayout.CENTER);
    }

    private void addListeners() {
        btnCollapseTree.addActionListener(evt -> tree.collapseTree());
        btnExpandTree.addActionListener(evt -> tree.expandTree());
    }
}
